jagexfandomcom-20200214-history
Example2.c
Example2.c was part of the Zip with commented example program in 3 stages guide that Andrew Gower made for his site around 1998. When you clicked to open the guide this and three other files would download onto your computer. This one read: ---- //This program is a modification of the previous one //it bounces the sprite around the screen now, and introduces //some MORE new stuff! //Things I explained in the last program aren't explained again, so //as not to completely swamp the program in comments, and make the new //stuff stand out more. //We need to include allegro before we can use it. #include //Global variables are setup here: BITMAP *mysprite; PALETTE colours; //The next 2 lines setup some more important variables //These are used to allow us to keep 2 copies of the screen //1 to draw to, and 1 to display - to prevent flicker. //To do this we need to create 2 screens. These are going to be stored in //page 1, and page 2. We also have 2 more pointers called physic and back //physic will be equal to either page1, or page2 and will be the screen we can see //back will be equal to either page1, or page2, it will be opposite to physic //this is the screen we write to. Finally we have one more variable called page //which we use to keep track of which way round physic and back are at any one time. int page=0; BITMAP *physic, *back, *page1, *page2; main() { //Local variables are setup here //We have some variables to keep track of the sprites position, and velocity. int n, x=0, y=0, xd=4, yd=4; //Setup the allegro graphics library allegro_init(); //Next we want to load in some graphics to mess about with mysprite=load_tga("graphic.tga",colours); if (mysprite 0) { printf("Error loading in graphic\n"); return 1; } //Set the computer to 256 colour mode set_color_depth(8); //Select a resolution of 640x480. set_gfx_mode(GFX_AUTODETECT,320,200,0,0); if (set_gfx_mode(GFX_AUTODETECT,640,480,640,480*2)!=0) { printf("Error setting video mode\n"); return 1; } //Set up the physic and back screens which work like in STOS //we see physic, and write to back. To setup these screens we //create a screen twice as big as the current resolution. We set //one half to page1, and the other half to page2 using the //allegro command: create_sub_bitmap page1=create_sub_bitmap(screen,0,0,640,480); page2=create_sub_bitmap(screen,0,480,640,480); //Call the screen_swap command before we start to make sure //everything is setup correctly. page=0; screen_swap(); //Set the colour palette for the screen set_palette(colours); //Main Program Loop Is Here! //This loops infintely so use CTRL-C to exit //While loops in C are fairly straightforward //The statement in the brackets is the condition to keep looping //The code to loop goes between the curly brackets. while(1 1) { //Clear the screen clear(back); //Now we want to draw something, so we plot //our sprite on the screen draw_sprite(back,mysprite,x,y); //Now bounce the sprite around the screen a bit //by changing its position. //Demonstates use of 'if' command in C //the 2 vertical bars (||) inside the brackets mean OR. x=x+xd; y=y+yd; if (x>640-64 || x<0) {xd=-xd;} if (y>480-64 || y<0) {yd=-yd;} //works just like the STOS command! //the function which does this is defined below. screen_swap(); } //Shut everything down neatly before we quit //This line never actually gets called at the moment, //but it's a good idea to put at here anyway //in case I ever change the program. allegro_exit(); } screen_swap() { //The page variable keeps track of which section of the //screen we can see. Each time this function is called //it swaps between 480 and 0. page=480-page; //This line selects which bit we can actually see. scroll_screen(0,page); //Now we need to update the physic and back variables so //they still correspond to the correct things, and work //as expected in the main program. if (page 0) {physic=page1; back=page2;} else {physic=page2; back=page1;} }